package com.cx.mall.demo.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SmsUtils {
    // 从本地yaml配置注入短信参数
    @Value("${sms.access-key}")
    private String accessKeyId;

    @Value("${sms.access-secret}")
    private String accessKeySecret;

    @Value("${sms.signature}")
    private String signName;

    @Value("${sms.template-code}")
    private String templateCode;

    // 发送短信方法
    public SendSmsResponse sendSms(String phoneNumber, String code) throws ClientException {
        // 初始化阿里云客户端（地域默认cn-hangzhou，短信服务支持）
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                accessKeyId,
                accessKeySecret
        );
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建短信请求
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber); // 接收手机号
        request.setSignName(signName); // 短信签名
        request.setTemplateCode(templateCode); // 模板ID
        String fakeMessage = "{\"project\":\""+"物探"+"\""+","+"\"testpoint\":\""+"1"+"\""+","+"\"reason\":\""+"aaa"+"\"}";
        //request.setTemplateParam("{\"code\":\"" + code + "\"}"); 因为模板问题暂时使用假数据测试，要发送验证码的话就使用这句
        request.setTemplateParam(fakeMessage); // 模板参数（验证码）

        // 发送短信并返回结果
        return client.getAcsResponse(request);
    }
}